package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.pojos.Task;
import cn.gson.springboot.model.service.TaskService;
import cn.gson.springboot.model.vo.TaskVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    HttpSession session;
    //分页查询集合
    @RequestMapping("/task-action-list")
    @ResponseBody
    public Map<String,Object> taskList(Integer page, Integer size,Integer value,Integer num,String content) {
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Task> tasks = taskService.selectTaskAction(emp.getDepartment().getDeptId(),emp.getEmpId(),value,num,content);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",tasks);
        return map;
    }
    //查询集合
    @RequestMapping("/task-action-all-list")
    @ResponseBody
    public List<Task> allTaskList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Task> tasks = taskService.selectTaskAction(emp.getDepartment().getDeptId(),emp.getEmpId(),null,null,null);
        return tasks;
    }

    //按cusId查询
    @RequestMapping("/task-action-cus-list")
    @ResponseBody
    public List<Task> actionCusList(Integer type,Integer cusId) {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Task> tasks = taskService.selectTaskActionByCusName(type,cusId);
        return tasks;
    }
    // 按机会Id查询、待办任务
    @RequestMapping("/task-opp-list")
    @ResponseBody
    public List<Task> allTaskOppList(Integer oppId){
        List<Task> tasks = taskService.selectTaskByOppId(oppId);
        return tasks;
    }
    // 按机会Id查询、待办任务
    @RequestMapping("/task-action-opp-list")
    @ResponseBody
    public List<Task> allActionOppList(Integer oppId){
        List<Task> tasks = taskService.selectTaskActionByOppId(oppId);
        return tasks;
    }

    // 查询待办任务与记录分布数据
    @RequestMapping("/com-act-task-list")
    @ResponseBody
    public List<Map<String,Object>> computeActionTaskList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeActionTask(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询记录信息类型分布数据
    @RequestMapping("/com-act-classify-list")
    @ResponseBody
    public List<Map<String,Object>> computeActionClassifyList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeActionClassify(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询待办任务状态分布数据
    @RequestMapping("/com-task-status-list")
    @ResponseBody
    public List<Map<String,Object>> computeTaskStatusList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeTaskStatus(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询待办任务优先度分布图数据
    @RequestMapping("/com-task-priority-list")
    @ResponseBody
    public List<Map<String,Object>> computeTaskPriorityList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeTaskPriority(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询数量月份统计柱状图数据
    @RequestMapping("/com-num-date-list")
    @ResponseBody
    public List<Map<String,Object>> computeNumAndDateList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeNumAndDate(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询客户联系次数柱状图数据
    @RequestMapping("/com-cus-num-list")
    @ResponseBody
    public List<Map<String,Object>> computeCusNumList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeCusNum(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询待办任务数量与月份折线图数据
    @RequestMapping("/com-task-num-date-list")
    @ResponseBody
    public List<Map<String,Object>> computeTaskNumAndDateList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeTaskNumAndDate(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    // 查询记录数量与月份折线图数据
    @RequestMapping("/com-act-num-date-list")
    @ResponseBody
    public List<Map<String,Object>> computeActionNumAndDateList(){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String, Object>> maps = taskService.computeActionNumAndDate(emp.getDepartment().getDeptId(),emp.getEmpId());
        return maps;
    }
    //查询员工待办任务
    @RequestMapping("/emp-task-status-list")
    @ResponseBody
    public Map<String,Object> empTaskStatusList(Integer page, Integer size,Integer taskStatus,Integer expired) {
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Task> tasks = taskService.selectEmpTaskByStatus(emp.getEmpId(),taskStatus,expired);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",tasks);
        return map;
    }
    //查询最近沟通未结束未过期的待办任务
    @RequestMapping("/emp-task-chat-list")
    @ResponseBody
    public Map<String,Object> empTaskChatList(Integer page, Integer size) {
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Task> tasks = taskService.selectEmpTaskByChat(emp.getEmpId());
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",tasks);
        return map;
    }
    //按ID查询待办任务
    @RequestMapping("/task-id")
    @ResponseBody
    public Task TaskId(Integer taskId) {
        Task tasks = taskService.selectTaskByTaskId(taskId);
        return tasks;
    }
    @RequestMapping("/save-task")
    @ResponseBody
    public void saveTask(@RequestBody TaskVo vo){
        taskService.saveTask(vo);
    }

    @RequestMapping("/save-action")
    @ResponseBody
    public void saveAction(@RequestBody TaskVo vo){
        taskService.saveAction(vo);
    }

    @RequestMapping("/delete-task")
    @ResponseBody
    public void deleteTask(@RequestParam(value = "taskId") Integer taskId){
        taskService.deleteTask(taskId);
    }
}
